import { createMemoryHistory, createRouter } from 'vue-router'

import IndexView from '../pages/index/index.vue'
import HomeView from '../pages/home/index.vue'
import UserView from '../pages/user/index.vue'
import LoginView from '../pages/login/index.vue'
import UserManage from '../pages/admin/UserManage.vue'
import ScoreManage from '../pages/admin/ScoreManage.vue'
import {useUserStore} from "@/store/userStore.js";
import StudentManage from "@/pages/admin/StudentManage.vue";
import TeacherManage from "@/pages/admin/TeacherManage.vue";
import AdminManage from "@/pages/admin/AdminManage.vue";

const routes = [
    { path: '/',name:'login', component: LoginView },
    {   
        path: '/index', 
        name: 'index',
        component:IndexView,
        children:[
            { 
                path: 'home', 
                name:'home',
                components: {
                    table:HomeView
                } 
            },
            {
                path: 'userCenter',
                name:'userCenter',
                components: {
                    table:UserView
                }
            },
            {
                path: 'score',
                name:'score',
                components: {
                    table:ScoreManage
                }
            },
            {
                path: 'user',
                name:'user',
                components: {
                    table:UserManage
                }
            },
            {
                path: 'studentManage',
                name:'studentManage',
                components: {
                    table:StudentManage
                }
            },
            {
                path: 'teacherManage',
                name:'teacherManage',
                components: {
                    table:TeacherManage
                }
            },
            {
                path: 'adminManage',
                name:'adminManage',
                components: {
                    table:AdminManage
                }
            }
        ]
    },
]


const router = createRouter({
  history: createMemoryHistory(),
  routes,
})

router.beforeEach(function (to, from, next) {
    //判断是否存在token值，不存在就去登录页面去登录
    let token = localStorage.getItem("token");
    if (localStorage.getItem("token")=='0') {
        if (to.name !== "login") {
            return next("/")
        }
    }
    //如果存在就执行下一步
    next()
})


export default router;